package dao;

import commonUtil.CommonConst;
import connection.JdbcConnection;
import dataValueObject.CardInfoValue;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static dao.CustomerInfoDAO.getNewId;
import static connection.JdbcConnection.getConn;

/**
 * Created by xiezy3 on 2016/8/9.
 */
public class CardInfoDAO {
    public static int insert(CardInfoValue cardInfo) {
        JdbcConnection jdbcConn = new JdbcConnection();
        Connection conn = getConn();
        int isSuccess = 0;
        String sql = "insert into card_info (card_id,card_type,use_times,expire_date,state,customer_id) values(?,?,?,?,?,?)";
        PreparedStatement pstmt;
        int newId = getNewId("card_id", "card_info");
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            pstmt.setInt(1, newId);
            pstmt.setString(2, cardInfo.getCardType());
            pstmt.setString(3, cardInfo.getUseTimes());
            pstmt.setString(4, cardInfo.getExpireDate());
            pstmt.setString(5, cardInfo.getState());
            pstmt.setInt(6, cardInfo.getCustomerId());

            isSuccess = pstmt.executeUpdate();
            jdbcConn.close(null, conn, pstmt);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //如果成功插入返回主键
        if (isSuccess > 0) {
            return newId;
        } else {
            return isSuccess;
        }
    }

    public static int deleteCust(CardInfoValue cardInfo) {
        JdbcConnection jdbcConn = new JdbcConnection();
        Connection conn = getConn();
        int isSuccess = 0;
        //将state值为0 失效
        String sql = "update card_info set state='" + CommonConst.State.IN_VALIDATE + "' where card_id='" + cardInfo.getCardId() + "'";
        PreparedStatement pstmt;
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            isSuccess = pstmt.executeUpdate();
            jdbcConn.close(null, conn, pstmt);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    public static CardInfoValue[] queryCardInfoByCustId(int custId) {
        JdbcConnection jdbcConn = new JdbcConnection();
        Connection conn = getConn();
        PreparedStatement pstmt;
        ArrayList<CardInfoValue> cardInfoList = new ArrayList<CardInfoValue>();
        String sql = "select * from card_info where customer_id = '" + custId + "'";
        try {
            pstmt = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                if (CommonConst.State.VALIDATE.equals(rs.getString("state"))) {
                    CardInfoValue cardInfo = new CardInfoValue();
                    cardInfo.setCardId(rs.getInt("card_id"));
                    cardInfo.setCardType(rs.getString("card_type"));

                    cardInfo.setExpireDate(rs.getDate("expire_date").toString());

                    cardInfo.setUseTimes(rs.getString("use_times"));

                    cardInfo.setState(CommonConst.State.VALIDATE);
                    cardInfoList.add(cardInfo);
                }
            }
            jdbcConn.close(rs, conn, pstmt);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return cardInfoList.toArray(new CardInfoValue[0]);
    }
}
